package ssi.staaar.messages;

import java.util.StringTokenizer;

public class RequestParser {
	public SynAckMessage parseSynAckMessage(String msg) {
		SynAckMessage sackMsg = new SynAckMessage();
		StringTokenizer st = new StringTokenizer(msg, "!-!");
		String tsC = st.nextToken();
		if (!tsC.equals(""))
			sackMsg.setTimestampClient(Long.parseLong(tsC));
		String tsS = st.nextToken();
		if (!tsS.equals(""))
			sackMsg.setTimestampServer(Long.parseLong(tsS));
		String sessId=st.nextToken();
		if (!sessId.equals(""))
			sackMsg.setSessionId(Integer.parseInt(sessId));
		String sessKey = st.nextToken();
		if (!sessKey.equals(""))
			sackMsg.setSessionKey(sessKey);
		return sackMsg;
	}

	public LowMessage parseMsgMessage(String msg) {
		StringTokenizer st = new StringTokenizer(msg, "!-!");
		LowMessage lmsg = new LowMessage();
		lmsg.setMsgId(Integer.parseInt(st.nextToken()));
		lmsg.setPayload(st.nextToken());
		return lmsg;
	}
	
	public KeyRecoveryResponseMessage parseRecoveryMessage(String msg) {
		KeyRecoveryResponseMessage msg1 = new KeyRecoveryResponseMessage();
		StringTokenizer to = new StringTokenizer(msg, "!-!");		
		msg1.setTimestamp(Long.parseLong(to.nextToken()));
		msg1.setPrivKey(to.nextToken());
		msg1.setPubKey(to.nextToken());
		return msg1;
	}
}
